﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using AssignmentSWEN3.App_Code;

namespace AssignmentSWEN3
{
    public partial class BookingViewEdit : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!IsPostBack)
            {
                DataRow dr = dbGetEvent(Request.QueryString["id"]);

                if (dr == null)
                {
                    throw new Exception("The event was not found");
                }

                StartTextBox.Text = Convert.ToDateTime(dr["eventstart"]).ToShortDateString();
                EndTextBox.Text = Convert.ToDateTime(dr["eventend"]).ToShortDateString();
                NameTextBox.Text = dr["name"] as string;

                DropDownRoomList.DataSource = dbGetResources();
                DropDownRoomList.DataTextField = "name";
                DropDownRoomList.DataValueField = "id";
                DropDownRoomList.SelectedValue = dr["resource_id"] as string;
                DropDownRoomList.DataBind();

                AddressTextBox.Text = dr["address"] as string;
                EmailTextBox.Text = dr["email"] as string;
                PhoneTextBox.Text = Convert.ToInt32(dr["phone"]).ToString();
                TextBoxAdult.Text = Convert.ToInt32(dr["Noadult"]).ToString();
                TextBoxChild.Text = Convert.ToInt32(dr["Nochildren"]).ToString();

                TextBox1.Text = dr["creditcardno"] as string;
                TextBox2.Text = Convert.ToDateTime(dr["creditcardexp"]).ToShortDateString();

                DropDownStatusList.SelectedValue = Convert.ToString(dr["status"]);

                NameTextBox.Focus();
            }
        }

        private DataTable dbGetResources()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name] FROM [resource]", ConnectionClass.Get(this));
            DataTable dt = new DataTable();
            da.Fill(dt);

            return dt;
        }

        private DataRow dbGetEvent(string id)
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [eventstart], [eventend], [resource_id], [address], [phone], [email], [Noadult], [Nochildren], [creditcardno], [creditcardexp], [status] FROM [event] WHERE id = @id", ConnectionClass.Get(this));
            da.SelectCommand.Parameters.AddWithValue("id", id);
            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0];
            }
            return null;
        }

        private void dbUpdateEvent(string id, DateTime start, DateTime end, string name, string resource, int status, string address, string email, int phone, int noadult, int nochildren, string creditcardno, DateTime creditcardexp)
        {
            using (SqlConnection con = new SqlConnection(ConnectionClass.Get(this)))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE [event] SET eventstart = @start, eventend = @end, name = @name, resource_id = @resource, address = @address, phone = @phone, email = @email, Noadult = @adult, Nochildren = @children, creditcardno = @creditnumber, creditcardexp = @creditcardexp, status = @status WHERE id = @id", con);
                cmd.Parameters.AddWithValue("id", id);
                cmd.Parameters.AddWithValue("start", start);
                cmd.Parameters.AddWithValue("end", end);
                cmd.Parameters.AddWithValue("name", name);
                cmd.Parameters.AddWithValue("resource", resource);
                cmd.Parameters.AddWithValue("status", status);
                cmd.Parameters.AddWithValue("address", address);
                cmd.Parameters.AddWithValue("phone", phone);
                cmd.Parameters.AddWithValue("email", email);
                cmd.Parameters.AddWithValue("adult", noadult);
                cmd.Parameters.AddWithValue("children", nochildren);
                cmd.Parameters.AddWithValue("creditnumber", creditcardno);
                cmd.Parameters.AddWithValue("creditcardexp", creditcardexp);
                cmd.ExecuteNonQuery();
            }
        }

        private void dbDeleteEvent(string id)
        {
            using (SqlConnection con = new SqlConnection(ConnectionClass.Get(this)))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("DELETE FROM [event] WHERE id = @id", con);
                cmd.Parameters.AddWithValue("id", id);
                cmd.ExecuteNonQuery();
            }
        }

        protected void ButtonOkay_Click(object sender, EventArgs e)
        {
            DateTime start = Convert.ToDateTime(StartTextBox.Text);
            DateTime end = Convert.ToDateTime(EndTextBox.Text);
            string name = NameTextBox.Text;
            string resource = DropDownRoomList.SelectedValue;
            string id = Request.QueryString["id"];
            int status = Convert.ToInt32(DropDownStatusList.SelectedValue);
            string address = AddressTextBox.Text;
            string email = EmailTextBox.Text;
            int phone = Convert.ToInt32(PhoneTextBox.Text);
            int noadult = Convert.ToInt32(TextBoxAdult.Text);
            int nochildren = Convert.ToInt32(TextBoxChild.Text);
            string creditcardno = TextBox1.Text;
            DateTime creditcardexp = Convert.ToDateTime(TextBox2.Text);

            dbUpdateEvent(id, start, end, name, resource, status, address, email, phone, noadult, nochildren, creditcardno, creditcardexp);
            ModalClass.Close(this, "OK");
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            ModalClass.Close(this);
        }

        protected void DeleteLinkButton_Click1(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
            dbDeleteEvent(id);
            ModalClass.Close(this, "OK");
        }

    }
}